package com.levine.base_libs.view;

import android.app.Activity;
import android.graphics.Color;
import android.os.Build;
import android.support.annotation.ColorRes;
import android.support.annotation.IntRange;
import android.support.annotation.LayoutRes;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.blankj.utilcode.util.BarUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.SizeUtils;
import com.levine.base.R;
import com.qmuiteam.qmui.widget.QMUITopBar;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

/**
 * 底层布局
 */
public class BaseView {
    public FrameLayout fl_top;
    public FrameLayout fl_body;
    public FrameLayout fl_bottom;
    public View statusMask;
    LinearLayout llParent;
    FrameLayout flBodyMask;
    private Activity mActivity;
    private View mView;
    public View top_bar;
    public QMUITopBar qmui_top_bar;
    public SmartRefreshLayout refreshLayout;
    public RecyclerView recyclerView;
    private View bodyView;
    private ViewGroup fragmentViewGroup;
    private LayoutInflater layoutInflater;
    private FrameLayout load_fl_top;
    private FrameLayout load_fl_bottom;
    private View view_load_status;
    private ReloadClickListener reloadClickListener;
    public FrameLayout fl_top_bar_bottom;

    public BaseView(Activity activity) {
        this(activity, null, null);
    }

    /**
     * 在fragment中使用此构造函数
     */
    public BaseView(Activity activity, LayoutInflater layoutInflater, ViewGroup fragmentViewGroup) {
        mActivity = activity;
        this.layoutInflater = layoutInflater;
        this.fragmentViewGroup = fragmentViewGroup;
        init();
    }

    private void init() {
        BarUtils.setStatusBarColor(mActivity, ContextCompat.getColor(mActivity, R.color.transparent), 0);
        initPerentView();
        initTopBar();
        initBody();
        initLoadView();
    }

    private void initPerentView() {
        mView = getLayout(R.layout.view_base, fragmentViewGroup);

        llParent = mView.findViewById(R.id.ll_parent);
        fl_top = mView.findViewById(R.id.b_fl_top);
        fl_body = mView.findViewById(R.id.b_fl_body);
        flBodyMask = mView.findViewById(R.id.fl_body_mask);
        fl_bottom = mView.findViewById(R.id.b_fl_bottom);
        statusMask = mView.findViewById(R.id.status_mask);

        statusMask.getLayoutParams().height = BarUtils.getStatusBarHeight();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
//            BarUtils.setStatusBarLightMode(mActivity, true);//设置状态栏文字颜色，默认true黑
        } else {
            statusMask.setBackgroundResource(R.color._28646464);
        }
    }

    private void initTopBar() {
        top_bar = getLayout(R.layout.view_base_top_bar);
        qmui_top_bar = top_bar.findViewById(R.id.b_top_bar);
        fl_top_bar_bottom = top_bar.findViewById(R.id.fl_top_bar_bottom);
        fl_top.addView(top_bar);
    }

    private void initBody() {
        bodyView = getLayout(R.layout.view_refresh_recyclerview);
        refreshLayout = bodyView.findViewById(R.id.refreshLayout);
        recyclerView = bodyView.findViewById(R.id.recyclerView);
//        refreshLayout.setRefreshHeader(new CustomRefreshHeader(mActivity));
        fl_body.addView(bodyView);
    }

    private void initLoadView() {
        view_load_status = getLayout(R.layout.view_load_status);
        load_fl_top = view_load_status.findViewById(R.id.fl_top);
        load_fl_bottom = view_load_status.findViewById(R.id.fl_bottom);

    }

    public void showEmptyView() {
        fl_body.setVisibility(View.GONE);
        flBodyMask.setVisibility(View.VISIBLE);
        flBodyMask.removeAllViews();
        View view = getLayout(R.layout.view_empty);
        flBodyMask.addView(view);
    }

    /**
     * 加载中
     */
    public void loading() {
        fl_body.setVisibility(View.GONE);
        flBodyMask.setVisibility(View.VISIBLE);
        ProgressBar progressBar = new ProgressBar(mActivity);
        load_fl_top.removeAllViews();
        load_fl_top.addView(progressBar);
        load_fl_bottom.setVisibility(View.GONE);
        flBodyMask.removeAllViews();
        flBodyMask.addView(view_load_status);
    }

    public void loadSuccess() {
        fl_body.setVisibility(View.VISIBLE);
        flBodyMask.setVisibility(View.GONE);
    }

    public void loadFail(String text) {
        fl_body.setVisibility(View.GONE);
        flBodyMask.setVisibility(View.VISIBLE);
        flBodyMask.removeAllViews();
        load_fl_top.removeAllViews();
        load_fl_bottom.removeAllViews();
        load_fl_bottom.setVisibility(View.VISIBLE);
        TextView textView = new TextView(mActivity);
        textView.setText(TextUtils.isEmpty(text) ? "加载失败" : text);
        load_fl_top.addView(textView);
        TextView tv_reload = new TextView(mActivity);
        tv_reload.setText("点击重试");
        tv_reload.setTextColor(Color.WHITE);
        tv_reload.setPadding(SizeUtils.dp2px(10),
                SizeUtils.dp2px(10),
                SizeUtils.dp2px(10),
                SizeUtils.dp2px(10));
//        tv_reload.setBackgroundResource(R.drawable.bg_15);
        tv_reload.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (reloadClickListener != null) {
                    reloadClickListener.onReload();
                }
            }
        });
        load_fl_bottom.addView(tv_reload);
        load_fl_bottom.setPadding(0, SizeUtils.dp2px(30), 0, 0);
        flBodyMask.addView(view_load_status);
    }

    public void loadFail() {
        loadFail("");
    }

    /**
     * 设置重新加载监听
     */
    public void setReloadClickListener(ReloadClickListener reloadClickListener) {
        this.reloadClickListener = reloadClickListener;
    }

    public interface ReloadClickListener {
        void onReload();
    }

    /**
     * 设置状态栏
     */
    public void setBar(boolean isImmersive,
                       @ColorRes int maskColorRes,
                       @IntRange(from = 0, to = 255) final int maskAlpha) {

        if (isImmersive) {
            BarUtils.subtractMarginTopEqualStatusBarHeight(fl_top);
        } else {
            BarUtils.addMarginTopEqualStatusBarHeight(fl_top);
        }
        statusMask.setBackgroundResource(maskColorRes);
        statusMask.getBackground().setAlpha(maskAlpha);
    }

    /**
     * 设置顶部导航栏
     *
     * @param view  添加的view，如果为null则顶部导航栏将删除所有子view，将处于看不见的状态
     * @param isAdd 是都是添加view
     */
    public void setTopBar(View view, boolean isAdd) {
        if (view == null) {
            fl_top.removeAllViews();
            return;
        }
        if (!isAdd) {
            fl_top.removeAllViews();
        }
        fl_top.addView(view);

    }

    public void setTopBar(View view) {
        setTopBar(view, false);
    }

    public void setTopBar(@LayoutRes int layoutId) {
        setTopBar(layoutId == -1 ? null : getLayout(layoutId), false);
    }

    /**
     * 设置主体
     *
     * @param layoutId 布局id
     * @param isAdd    是否添加
     */
    public void setBodyView(int layoutId, boolean isAdd) {
        View view = getLayout(layoutId);
        setBodyView(view, isAdd);
    }

    public void setBodyView(View view, boolean isAdd) {
        if (!isAdd) {
            fl_body.removeAllViews();
        }
        fl_body.addView(view);
    }

    public void setBodyView(int layoutId) {
        setBodyView(layoutId, false);
    }

    public void setBodyView(View view) {
        setBodyView(view, false);
    }

    /*设置底部view*/
    public void setBottomView(View view, boolean isAdd) {
        if (!isAdd) {
            fl_bottom.removeAllViews();
        }
        fl_bottom.addView(view);
    }

    public void setBottomView(@LayoutRes int layoutRes, boolean isAdd) {
        setBottomView(getLayout(layoutRes), true);
    }

    public void setBottomView(@LayoutRes int layoutRes) {
        setBottomView(getLayout(layoutRes), false);
    }

    public void setBottomView(View view) {
        setBottomView(view, false);
    }


    private View getLayout(int layoutId, ViewGroup viewGroup) {
        View view;
        LayoutInflater layoutInflater = mActivity.getLayoutInflater();

        if (viewGroup == null) {
            view = layoutInflater.inflate(layoutId, null);
        } else {
            view = this.layoutInflater.inflate(layoutId, fragmentViewGroup, false);
        }

        return view;
    }

    private View getLayout(int layoutId) {
        return getLayout(layoutId, null);
    }

    public View getView() {
        return mView;
    }
}
